﻿using BlogDemo.Application.PostApp.Resource;
using BlogDemo.Common;
using BlogDemo.Core.Domain;
using BlogDemo.Core.Repository;
using BlogDemo.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogDemo.Application.PostApp
{
    public class PostAppService : IPostAppService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IPostRepostory postRepostory;
        private readonly IBlogRepostory blogRepostory;
        public PostAppService(IUnitOfWork unitOfWork, IBlogRepostory blogRepostory,
            IPostRepostory postRepostory)
        {
            this.unitOfWork = unitOfWork;
            this.blogRepostory = blogRepostory;
            this.postRepostory = postRepostory;
        }

        public void Add(PostAddResourceModel postAdd)
        {
            var entity = postAdd.MapTo<Post>();

            this.postRepostory.Add(entity);
            this.unitOfWork.Commit();
        }

        public PostAddResourceModel Get(int postId)
        {
            var entity = this.postRepostory.Find(x => x.Id == postId);
            return entity.MapTo<Post, PostAddResourceModel>();

        }

        public IEnumerable<PostAddResourceModel> GetAll(int blogId)
        {
            var entities = this.postRepostory.Query(x => x.BlogId == blogId);
            return entities.MapTo<Post, PostAddResourceModel>();
        }

        public bool IsBlogExist(int blogId)
        {
            return blogRepostory.Any(c => c.Id == blogId);
        }
    }
}
